package edu.gyc.appshiro.controller;


import edu.gyc.appshiro.model.Role;
import edu.gyc.appshiro.model.User;
import edu.gyc.appshiro.service.RoleService;
import edu.gyc.appshiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("/query")
    public String query(Model model) {
        List<Role> roles = roleService.list();
        model.addAttribute("roles", roles);
        return "/admin/role";
    }

    @RequestMapping("/queryperms")
    public String queryperms(Integer id, Model model) {

        model.addAttribute("roleId", id);
        model.addAttribute("roleName", roleService.lambdaQuery().eq(Role::getId, id).one().getDescription());

        model.addAttribute("myperms", roleService.getPermsByRoleId(id));
        return "/admin/myperms";
    }

    @PostMapping("/updateRolePermissions")
    public String form1(String[] acquireId, Integer roleId, Model model) {
        String a = Arrays.toString(acquireId);
        List<Integer> ids = new ArrayList<>();
        if (acquireId.length > 0) {
            for (String id : acquireId) {
                ids.add(Integer.parseInt(id));
            }
        }

        roleService.updateRolePermissions(roleId, ids);


        model.addAttribute("qxmsg", "角色权限更新成功！");
        model.addAttribute("roleId", roleId);

        return "/admin/permmsg";
    }

    @Autowired
    UserService userService;

    @GetMapping("/addUser")
    public String adduser(Integer rid, Model model) {
        Role role = roleService.getById(rid);
        List<User> users=userService.list();
        model.addAttribute("role", role);
        model.addAttribute("users", users);
        return "/admin/roleuser";
    }

    @PostMapping("/addUser")
    public String doadduser(Integer rid, Integer uid, Model model) {
        boolean r =  roleService.addRoleUser(rid,uid);
        String msg = "";
        if(r){
            msg = "添加角色成员成功";
        }else {
            msg = "成员已拥有该角色";
        }

        model.addAttribute("msg", msg);
        return "/admin/rolemsg";
    }

    @PostMapping("/add")
    public String add(Role role, Model model) {
        boolean r =  roleService.addRole(role);
        String msg = "";
        if(r){
            msg = "添加角色成功";
        }else {
            msg = "已拥有该角色";
        }

        model.addAttribute("msg", msg);
        return "/admin/rolemsg";
    }

    @GetMapping("/del")
    public String del(Integer rid, Model model) {
       boolean r= roleService.delRole(rid);

        String msg = "";
        if(r){
            msg = "删除角色成功";
        }else {
            msg = "删除角色失败";
        }
        model.addAttribute("msg", msg);
        return "/admin/rolemsg";
    }

}

